package com.lnsf.bookstore.web.category.controller;

import com.github.pagehelper.PageInfo;
import com.lnsf.bookstore.common.annotation.IgnoreAuth;
import com.lnsf.bookstore.common.controller.ApiBaseAction;
import com.lnsf.bookstore.web.category.service.ApiCategoryService;
import com.lnsf.bookstore.web.category.vo.ApiCategory;
import com.lnsf.bookstore.web.user.vo.ApiUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/category")
public class ApiCategoryController extends ApiBaseAction{


    @Autowired
    private ApiCategoryService apiCategoryService;

    @RequestMapping("/index")
    @ResponseBody
    @IgnoreAuth
    public Object index(ApiUser loginApiUser, Integer id,
                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                        @RequestParam(value = "size", defaultValue = "10") Integer size) {

        Map<String, Object> resultObj = new HashMap();
        Map params = new HashMap();
        params.put("page", page);
        params.put("limit", size);
        params.put("parentCode", 0);
        params.put("order", "desc");
        //查询列表数据，第一层分类信息
        PageInfo<ApiCategory> apiCategoryList = apiCategoryService.queryList(params);

        //查询当前选中的分类
        ApiCategory currentApiCategory = null;
        if (null != id) {
            currentApiCategory = apiCategoryService.queryObject(id);
        }
        if(null == id){
            currentApiCategory = apiCategoryList.getList().get(0);
        }
        //获取子分类数据
        if (null != currentApiCategory && null != currentApiCategory.getId()) {
            params.put("parentCode", currentApiCategory.getCode());
            currentApiCategory.setSubCategoryList(apiCategoryService.queryList(params).getList());
        }

        resultObj.put("categoryList", apiCategoryList.getList());
        resultObj.put("currentCategory", currentApiCategory);


        return toResponsSuccess(resultObj);

    }


    @RequestMapping("/current")
    @ResponseBody
    @IgnoreAuth
    public Object current(ApiUser loginApiUser, Integer id) {
        Map<String, Object> resultObj = new HashMap();
        Map params = new HashMap();
        params.put("parentCode", 0);
        ApiCategory currentApiCategory = null;
        if (null != id) {
            currentApiCategory = apiCategoryService.queryObject(id);
        }
        //获取子分类数据
        if (null != currentApiCategory && null != currentApiCategory.getId()) {
            params.put("parentCode", currentApiCategory.getCode());
            currentApiCategory.setSubCategoryList(apiCategoryService.queryList(params).getList());
        }
        resultObj.put("currentCategory", currentApiCategory);
        return toResponsSuccess(resultObj);
    }
}
